package xiaohu.day6.HJ70;

import java.util.Scanner;
import java.util.Stack;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int n = scanner.nextInt();
            int[][] arr = new int[n][2];
            for (int i=0;i<n;i++){
                arr[i][0]=scanner.nextInt();
                arr[i][1]=scanner.nextInt();
            }
            scanner.nextLine();
            String str = scanner.nextLine();
            System.out.println(timesOfMatrixMultiplication(str, arr));
        }
    }
    private static int timesOfMatrixMultiplication(String str, int[][] arr) {
        int count = 0;
        int total = 0;
        Stack<Integer> stack = new Stack<Integer>();
        for (char c : str.toCharArray()) {
            if (c == '('){
                count++;
            }else if (c == ')'){
                count--;
                int x = stack.pop();
                int y = stack.pop();
                total +=arr[y][0]*arr[y][1]*arr[x][1] ;
                arr[y][1]=arr[x][1];
                stack.add(y);
            }else{
                int i = c-'A';
                stack.add(i);
            }
        }

        return total;
    }
}
